<?php
/**
 * Created by PhpStorm.
 * User: itboye
 * Date: 2016/12/1
 * Time: 17:09
 */
namespace Admin\Controller;

class IntentionalOrderController extends AdminController {
    public function index(){
        if(IS_GET){
            $r = apiCall('Admin/IntentionalOrder/query',array());
            $this->assign('list',$r['info']['list']);
            $this->display();
        }else{
            //get.startdatetime
            $startdatetime = I('startdatetime',date('Y/m/d H:i',time()-24*3600),'urldecode');
            $enddatetime = I('enddatetime',date('Y/m/d H:i',time()),'urldecode');

            //分页时带参数get参数
            $params = array(
                'startdatetime'=>$startdatetime,
                'enddatetime'=>$enddatetime
            );

            $startdatetime = strtotime($startdatetime);
            $enddatetime = strtotime($enddatetime);

            if($startdatetime === FALSE || $enddatetime === FALSE){
                LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
                $this->error(L('ERR_DATE_INVALID'));
            }

            $map = array();
            $map['p.update_time'] = array(array('EGT',$startdatetime),array('ELT',$enddatetime),'and');
            $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
            $order = "update_time desc ";
            $result = apiCall('Admin/IntentionalOrder/query',array($map,$page,$order,$params));
//           dump($result);exit();
            if($result['status']){
                $this->assign('startdatetime',date('Y-m-d H:i:s',$startdatetime));
                $this->assign('enddatetime',date('Y-m-d H:i:s',$enddatetime));
                $this->assign('show',$result['info']['show']);
                $this->assign('list',$result['info']['list']);
                $this->display();
            }else{
                LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
                $this->error(L('UNKNOWN_ERR'));
            }
        }
    }
    public function detail(){
        $map = array(
            'id'=>I('param.id')
        );
        $r = apiCall('Admin/IntentionalOrder/query',array($map));
        $this->assign('info',$r['info']['list'][0]);
        $list = json_decode($r['info']['list'][0]["order_items"],false);
        $li = $this->object_to_array($list[0]);
        $this->assign('list',$li);
        $this->display();
    }
    public function del(){
        $map = array(
            'id'=>I('param.id')
        );
        $r = apiCall('Admin/IntentionalOrder/delete',array($map));
        if ($r['status']) {
            $this -> success("删除成功！", U('Admin/IntentionalOrder/index'));
        } else {
            $this -> error($result['info']);
        }
    }
    function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v)
        {
            if (gettype($v) == 'resource')
            {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array')
            {
                $obj[$k] = (array)object_to_array($v);
            }
        }

        return $obj;
    }
}